package com.tabjin.rfidsocket.controller.v1.sys;

import com.tabjin.rfidsocket.common.utils.PagedGridResult;
import com.tabjin.rfidsocket.common.utils.TabJsonResult;
import com.tabjin.rfidsocket.pojo.SysAcl;
import com.tabjin.rfidsocket.pojo.bo.SysAclBO;
import com.tabjin.rfidsocket.pojo.vo.SysAclVO;
import com.tabjin.rfidsocket.pojo.vo.SysRoleVO;
import com.tabjin.rfidsocket.service.sys.SysAclService;
import com.tabjin.rfidsocket.service.sys.SysRoleService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author tabjin
 * create at 2020/12/22 16:28
 * @program rfid_socket
 * @description 权限点
 */
@RestController
@RequestMapping(value = "/sys/acl")
public class SysAclController {
    private final SysAclService sysAclService;
    private final SysRoleService sysRoleService;

    public SysAclController(SysAclService sysAclService, SysRoleService sysRoleService) {
        this.sysAclService = sysAclService;
        this.sysRoleService = sysRoleService;
    }

    @RequestMapping(value = "/acl.page")
    public ModelAndView page() {
        return new ModelAndView("Acl");// 需要新的页面aclModule
    }

    /**
     * 新增权限点
     *
     * @param sysAclBO
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult add(@RequestBody SysAclBO sysAclBO) {
        SysAclVO vo = sysAclService.add(sysAclBO);
        return TabJsonResult.ok(vo);
    }

    /**
     * 更新权限点
     *
     * @param sysAclBO
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult update(@RequestBody SysAclBO sysAclBO) {
        SysAclVO vo = sysAclService.update(sysAclBO);
        return TabJsonResult.ok(vo);
    }

    /**
     * 权限模块下的权限点
     *
     * @param sysAclModuleId
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/aclsByAclModule", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult list(@RequestParam(value = "aclModuleId", required = true) Integer sysAclModuleId,
                              @RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                              @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        PagedGridResult pagedGridResult = sysAclService.searchAclModuleItems(sysAclModuleId, page, pageSize);
        return TabJsonResult.ok(pagedGridResult);
    }

    @RequestMapping(value = "/aclsByAclModules", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult listWithoutPage(@RequestParam(value = "aclModuleIds", required = true) String strSysAclModuleIds) {
        List<String> sysAclModuleIds = Arrays.asList(strSysAclModuleIds.split(","));
        List<Integer> sysAclModuleIdList = sysAclModuleIds.stream().map((item) -> {
            return Integer.valueOf(item);
        }).collect(Collectors.toList());
        List<SysAclVO> sysAclList = sysAclService.searchAclModuleItemsWithoutPaged(sysAclModuleIdList);
        return TabJsonResult.ok(sysAclList);
    }

    /**
     * 权限点对应的角色和用户
     *
     * @param aclId
     * @return
     */
    @RequestMapping(value = "/items", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult items(@RequestParam(value = "aclId", required = true) Integer aclId) {
        // TODO
        Map<String, Object> map = new HashMap<>();
        List<SysRoleVO> roleVOList = sysRoleService.getRoleListByAclId(aclId);
        map.put("roles", roleVOList);// 权限已分配的角色，正常权限是先和角色关联，然后角色和用户关联
        map.put("users", sysRoleService.getUserListByRoleList(roleVOList));// 权限已分配的用户
        return TabJsonResult.ok(map);
    }
}
